package com.dehui.traders.ui.stockdetail.chart;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.dehui.traders.R;
import com.dehui.traders.bean.StockMinuteResponse;
import com.dehui.traders.bean.Tick;
import com.dehui.traders.kchart.DataParse;
import com.dehui.traders.kchart.MyCombinedChartX;
import com.dehui.traders.kchart.PriceManager;
import com.dehui.traders.kchart.XVolumeManager;
import com.dehui.traders.service.CallListener;
import com.dehui.traders.service.NetImpl;
import com.dehui.traders.ui.stockdetail.StocksDetailActivity;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class FenShiDetailFragment extends Fragment {

    private View view;
    private MyCombinedChartX mChartPrice;
    private MyCombinedChartX mChartVolue;
    private String code;
    private String market;
    private DataParse mData;
    private String close;
    private LinearLayout mLlChartFenshi;
    private RelativeLayout rlLoading;
    private StocksDetailActivity mActivity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (view == null)
            view = inflater.inflate(R.layout.fragment_detail_fenshi,null);
        else{
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null)
                parent.removeView(view);
        }


        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViews();
        mActivity = (StocksDetailActivity) getActivity();
        Bundle bundle = getArguments();
        code = bundle.getString("code");
        market = bundle.getString("market");
        close = bundle.getString("close");
        init();
        initData();
    }

    private void initViews() {
        mChartPrice = view.findViewById(R.id.mycombinedchart_price);
        mChartVolue = view.findViewById(R.id.mycombinedchart_volume);
        mLlChartFenshi = view.findViewById(R.id.ll_chart_fenshi);
        rlLoading = view.findViewById(R.id.rl_loading);
    }

    private void initData() {
        getTick();
    }

    /**
     * 获取分时图数据
     */
    public void getTick() {
        Call<StockMinuteResponse> call = NetImpl.getTodayTicks1(market + code);
        call.enqueue(new CallListener<StockMinuteResponse>() {
            @Override
            public void _onResponse(Response<StockMinuteResponse> response) {
                StockMinuteResponse stockMinuteResponse = response.body();
                List<Tick> tickList = stockMinuteResponse.result;
                initChartMinite(tickList);
            }

            @Override
            public void _onFailure() {

            }
        });
    }

    public void initChartMinite(List<Tick> list) {
        rlLoading.setVisibility(View.GONE);
        mLlChartFenshi.setVisibility(View.VISIBLE);
        mData = new DataParse();
        mData.parseMinuteList(list,Float.valueOf(close));
        PriceManager.getInstange().initPrice(getActivity(),mChartPrice, mData,true);
        XVolumeManager.getInstange().initVolume(getActivity(),mChartVolue, mData,true);
    }

    private void init() {
        initChartListener();
    }

    private void initChartListener() {
        mChartPrice.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
                Highlight highlight = new Highlight(h.getXIndex(), h.getValue(), h.getDataIndex(), h.getDataSetIndex());

                float touchY = h.getTouchY() - mChartPrice.getHeight();
                Highlight h1 = mChartPrice.getHighlightByTouchPoint(h.getXIndex(), touchY);
                highlight.setTouchY(touchY);
                if (null == h1) {
                    highlight.setTouchYValue(0);
                } else {
                    highlight.setTouchYValue(h1.getTouchYValue());
                }
                mChartVolue.highlightValues(new Highlight[]{highlight});

                mActivity.showMinuteView(h.getXIndex(),mData);
            }

            @Override
            public void onNothingSelected() {
                mChartVolue.highlightValue(null);
                mActivity.dimissMinuteView();
            }
        });

        mChartVolue.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
                Highlight highlight = new Highlight(h.getXIndex(), h.getValue(), h.getDataIndex(), h.getDataSetIndex());

                float touchY = h.getTouchY() + mChartPrice.getHeight();
                Highlight h1 = mChartPrice.getHighlightByTouchPoint(h.getXIndex(), touchY);
                highlight.setTouchY(touchY);
                if (null == h1) {
                    highlight.setTouchYValue(0);
                } else {
                    highlight.setTouchYValue(h1.getTouchYValue());
                }
                mChartPrice.highlightValues(new Highlight[]{highlight});
                mChartVolue.highlightValues(new Highlight[]{highlight});
            }

            @Override
            public void onNothingSelected() {
                mChartPrice.highlightValue(null);
                mActivity.dimissMinuteView();
            }
        });


    }

}
